import { xhrPostJson } from '@/services';
import request from '@ey-utils/utils/services/request';
import _ from 'lodash';
import api from '../api';
// 公用分页查询
// 分页查询前端组件
export const getPage = async (url: string, queryKeys: any, pageNo: number, pageSize: number) =>
  await request(url, {
    method: 'post',
    data: {
      pageNo,
      pageSize,
      queryKeys,
    },
  }).then((res) => res);
// 获取模型页面信息
export const getPageMeta = async (
  pageCode: string,
  remoteUrl?: string,
  remoteParams?: any,
  method: string = 'get',
) =>
  await request(remoteUrl || api.getPageConfigByPageCode, {
    params: {
      pageCode,
      ...(remoteParams || {}),
    },
    method,
  }).then((res) => res).catch(err => err);
// 获取模型页面信息
export const getPageMetaMap = async (keys: string[]) =>
  await xhrPostJson(api.pageConfigMap, keys).then((res) => res);
/**
 *  King
 */
export const create = async (values: any) =>
  await request(api.metaCreate, {
    method: 'post',
    data: values,
  }).then((res) => res);
export const update = async (values: any) =>
  await request(api.metaUpdate, {
    method: 'post',
    data: values,
  }).then((res) => res);
export const detail = async (id: string) =>
  await request(api.metaDetail, {
    method: 'get',
    params: { id },
  }).then((res) => res);
export const page = async (queryKeys: any, pageNo: number, pageSize: number) =>
  await request(api.metaPage, {
    method: 'post',
    data: {
      mdType: 'Meta',
      pageNo,
      pageSize,
      queryKeys,
    },
  }).then((res) => res);
export const remove = async (id: string) =>
  await request(api.metaRemove, {
    method: 'get',
    params: { id },
  }).then((res) => res);

export const listByName = async (name: string) =>
  await request(api.metaListByName, {
    method: 'get',
    params: {
      name,
    },
  }).then((res) => res);

export const listByIds = async (ids: string[]) =>
  await request(api.metaListByIds, {
    method: 'post',
    data: ids,
  }).then((res) => res);

// 获取通用按钮页面信息
export const getButtonPageMeta = async () =>
  await request(api.getPageConfigByPageCode, {
    params: {
      pageCode: 'GENERAL_BUTTON_PAGE',
    },
  }).then((res: any) => {
    const pageDatas: any[] = res?.result?.pageDatas;
    if (pageDatas) {
      return _.map(pageDatas, 'key');
    }
  });
